package org.pp.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.pp.admin.dto.AuthUpate;
import org.pp.admin.dto.SysRoleAdd;
import org.pp.admin.dto.SysRoleEdite;
import org.pp.admin.dto.SysRolePage;
import org.pp.annotations.Permission;
import org.pp.core.PageBean;
import org.pp.core.Result;
import org.pp.entity.SysRole;
import org.pp.service.ISysRoleMenuService;
import org.pp.service.ISysRoleService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author haima
 * @since 2024-04-20
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends
        BaseCrudController<SysRole, SysRoleAdd, SysRoleEdite,SysRole>
implements BaseAdd<SysRoleAdd>,
        BaseEdite<SysRoleEdite>,
        BasePage<SysRole>,
        BaseDelete
{

    @Autowired
    ISysRoleMenuService roleMenuService;

    @Autowired
    ISysRoleService roleService;


    @Override
    @Permission("post:/sys/role/add")
    public Result<Object> add(SysRoleAdd dto) {
        return super.add(dto);
    }

    @Override
    @Permission("post:/sys/role/delete")
    public Result<Void> delete(String ids) {
        //return super.delete(ids);
        roleService.delete(ids);
        return Result.ok();
    }

    @Override
    @Permission("post:/sys/role/edite")
    public Result<Void> edite(SysRoleEdite dto) {
        return super.edite(dto);
    }



    @Permission("get:/sys/role/list")
    @GetMapping("/list")
    @Operation(summary = "CRUD操作 分页查询")
    public Result<PageBean<SysRole>> page(@ParameterObject SysRolePage page) {
        return super.page(page);
    }


    @GetMapping("/auth/get")
    @Operation(summary = "获取角色的权限id")
    @Permission("get:/sys/role/auth/get")
    public Result<List<Long>> authGet(@RequestParam(name = "roleId",required =true) Long roleId ){
        List<Long> menuIds=  roleService.authGet(roleId);
        return Result.ok(menuIds);
    }

    @PostMapping("/auth/update")
    @Permission("post:/sys/role/auth/update")
    @Operation(summary = "为角色授权")
    public Result<Void> authUpate(@RequestBody @Validated AuthUpate dto){
        roleService.authUpate(dto);
        return Result.ok();
    }


}
